package com.mss.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mss.media.dto.AuditProfessionalDTO;
import com.mss.media.dto.PageDTO;
import com.mss.media.entity.ProfessionalAuditList;
import com.mss.media.mapper.ProfessionalAuditListMapper;
import com.mss.media.service.IMediaAdminService;
import com.mss.media.service.IProfessionalAuditListService;
import com.mss.media.util.abnormal.MssException;
import com.mss.media.vo.ProfessionalAuditPageVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户职业审核列表  服务实现类
 * </p>
 *
 * @author runqiang
 * @since 2021-12-01
 */
@Service
public class ProfessionalAuditListServiceImpl extends ServiceImpl<ProfessionalAuditListMapper, ProfessionalAuditList> implements IProfessionalAuditListService {

    @Autowired
    private IMediaAdminService mediaAdminService;
    @Autowired
    private ProfessionalAuditListMapper professionalAuditListMapper;

    /**
     * 查询职业审核列表
     *
     * @param dto 分页信息
     * @return 分页数据
     */
    @Override
    public IPage<ProfessionalAuditPageVO> findProfessionalAuditPage(PageDTO dto) {
        if (ObjectUtils.isEmpty(dto)) {
            dto = new PageDTO();
            dto.setLimit(10)
                    .setPage(1);
        }
        if (dto.getPage() == null) {
            dto.setPage(1);
        }
        if (dto.getLimit() == null) {
            dto.setLimit(10);
        }
        Page<ProfessionalAuditPageVO> page = new Page<>(dto.getPage(), dto.getLimit());
        return professionalAuditListMapper.ProfessionalAuditPage(dto.getAudit(), dto.getSubmitTime(), dto.getAuditTime(), page);

    }

    /**
     * 审核职业申请
     *
     * @param dto     审核信息
     * @param request 请求头
     * @return 状态
     */
    @Override
    public Boolean auditProfessional(AuditProfessionalDTO dto, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(dto)) {
            throw new MssException("参数不能为空");
        }
        LambdaQueryWrapper<ProfessionalAuditList> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProfessionalAuditList::getProfessionalId, dto.getProfessionalId());
        ProfessionalAuditList professionalAuditList = professionalAuditListMapper.selectOne(queryWrapper);
        String adminAccount = mediaAdminService.parsingAccount(request);
        LocalDateTime now = LocalDateTime.now();
        LambdaUpdateWrapper<ProfessionalAuditList> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(ProfessionalAuditList::getAuditTime, now);
        wrapper.set(ProfessionalAuditList::getAdminAccount, adminAccount);
        wrapper.set(ProfessionalAuditList::getAuditStatus, dto.getAuditStatus());
        if (dto.getAuditStatus() == 2) {
            wrapper.set(ProfessionalAuditList::getNotPassCause, dto.getNotPassCause());
        }
        wrapper.eq(ProfessionalAuditList::getProfessionalId, dto.getProfessionalId());
        int update = professionalAuditListMapper.update(professionalAuditList, wrapper);
        if (update <= 0) {
            throw new MssException("系统错误，更改失败");
        }
        return true;
    }
}
